package com.example.recuitactivity;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageButton;

import com.example.recuitactivity.adapter.RecruitAllInfoAdapter;
import com.example.recuitactivity.bean.RecuitInfo;
import com.example.recuitactivity.helper.DbOpenHelper;

import java.util.ArrayList;
import java.util.List;

public class CollectionActivity extends AppCompatActivity {
    private RecyclerView rv;
    private ImageButton ib_back;
    private List<RecuitInfo> mDatas=new ArrayList<>();
    private RecruitAllInfoAdapter adapter;
    private DbOpenHelper dbOpenHelper;
    private String flag="CollectionActivity";
    private String username;

    private View.OnClickListener listener=new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            switch (view.getId()){
                case R.id.ib_back2:
                    finish();
                    break;
            }
        }
    };

    public void initRecyclerView(){
        LinearLayoutManager layoutManager=new LinearLayoutManager(CollectionActivity.this);
        rv.setLayoutManager(layoutManager);
        adapter=new RecruitAllInfoAdapter(CollectionActivity.this,mDatas,flag,username);
        rv.setAdapter(adapter);
    }


    public void initUI() {
        dbOpenHelper=new DbOpenHelper(CollectionActivity.this);
        rv=(RecyclerView) findViewById(R.id.rv_collection_allinfo);
        ib_back=(ImageButton) findViewById(R.id.ib_back2);
        ib_back.setOnClickListener(listener);

    }

    public void getDatas(){
        List<RecuitInfo> list=new ArrayList<>();
        Intent intent=getIntent();
        Bundle bundle=intent.getExtras();
        long cid=bundle.getLong("candidates_id");
        username=bundle.getString("username");
        List<Long> pidList=dbOpenHelper.getCollectsPidByCid(cid);

        for(int i=0;i<pidList.size();i++){
            RecuitInfo recuitInfo=dbOpenHelper.getRecruitInfoById(pidList.get(i));
            list.add(recuitInfo);
        }
        mDatas=list;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_collection);
        initUI();
        getDatas();
        initRecyclerView();
    }

    @Override
    public void onResume() {
        super.onResume();
        getDatas();
        adapter.setmDatas(mDatas);
    }
}